#include "Input.h"

void Input::Initialize(HWND WindowHandle, HINSTANCE hInstance)
{
	DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&DirectInput, NULL);
	DirectInput->CreateDevice(GUID_SysKeyboard, &KeyBoard, NULL);
	DirectInput->CreateDevice(GUID_SysMouse, &Mouse, NULL);
	KeyBoard->SetDataFormat(&c_dfDIKeyboard);
	Mouse->SetDataFormat(&c_dfDIMouse);
	KeyBoard->SetCooperativeLevel(WindowHandle, DISCL_FOREGROUND | DISCL_EXCLUSIVE);
	Mouse->SetCooperativeLevel(WindowHandle, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
	KeyBoard->Acquire();
	Mouse->Acquire();
};

void Input::Update()
{

	KeyBoard->Acquire();	
	KeyBoard->GetDeviceState(sizeof(KeyboardState), (LPVOID)&KeyboardState);
	Mouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&MouseState);

};

bool Input::KeyDown(int key)
{
	if (KeyboardState[key] & 0x80)
	{
		return true;
	}
	else
	{
		return false;
	}
};

DirectX::XMFLOAT2 Input::MouseMove()
{
	DirectX::XMFLOAT2 temp;
	temp.x = (float)MouseState.lX;
	temp.y = (float)MouseState.lY;
	return temp;
}


void Input::Shutdown()
{
	 // Release the keyboard.
        if(KeyBoard)
        {
                KeyBoard->Unacquire();
                KeyBoard->Release();
                KeyBoard = 0;
        }

        // Release the main interface to direct input.
        if(DirectInput)
        {
                DirectInput->Release();
                DirectInput = 0;
        }
};